<?php

/**
* 站内信
*
* @package		Hooloo framework
* @author 		Bill
* @copyright 	Hooloo Co.,Ltd
* @version		1.0
* @release		2017.05.25
*/
defined('BASEPATH') OR exit('No direct script access allowed');

class Message  extends Controller{
	public function __construct(){
		parent::__construct();
	}
	
	
	/**
	* 站内信列表 管理员发给用户
	*/
	public function mail($start_time = 0 ,$end_time = 0 , $p = 1 ){
		//指定代理商
		$aid = $_SESSION["agent"]["id"];
		
		//只搜索管理员发个普通用户的
		$where = "where a.sender = 0 AND a.receiver = b.id and b.aid = $aid";
		
		//搜索时间
		if (! $start_time) $start_time = date("Y-m-d"); 
		if (! $end_time) $end_time = date("Y-m-d"); 
		if ($start_time > $end_time) $this->error("开始时间不能大于结束时间！");
		$where .= " and a.addtime >= '".$start_time."' and a.addtime <= '".$end_time . " 23:59:59"."'";
		
		//数据分页
		$p = (int)$p <= 0 ? 1 : (int)$p;
		$pize = 15;
		$start = ($p - 1) * $pize;
		
		//统计总数
		$sql = "select count(*) as c from message a,user b " . $where;
		$total = $this->db->query($sql)->row_array();
		$total = $total["c"];

		//分页查询
		$sql = "select a.*,b.username,b.realname  from message a,user b " . $where . " order by a.id DESC limit $start, $pize";
		$list = $this->db->query($sql)->result_array();
		
		//分页展示
		$url = "/" . $this->_controller . "/" . $this->_action  . "/" . $start_time. "/" . $end_time . "/";
		$page = page_format($total, $pize, $p, $url);
		
		//分配展示
		$this->assign("list", $list);
		$this->assign("page", $page);
		$this->assign("total", $total);
		$this->assign("start_time", $start_time);
		$this->assign("end_time", $end_time);
		$this->display();
	}

	/**
	* 
	* @param  $id 消息ID
	* 
	* @return
	*/
	public function mail_detail($id=0){
		//指定代理商
		$aid = $_SESSION["agent"]["id"];
		!$id &&	$this->error("参数有误");
		
		//查询记录
		$sql = "select a.*,b.username from message a,user b  where a.id = $id and a.receiver=b.id and b.aid = $aid";
		$result = $this->db->query($sql)->row_array();
		if(! $result){
			$this->error("记录不存在");
		}
		
		//更新阅读状态
		$sql = "update message set isread = 1 where id = $id and isread = 0";
		$this->db->query($sql);
		
		$this->assign("result", $result);
		$this->display();
		
	}
	
	/**
	* 删除短信通知
	* @param    int  	$id   	通知id
	*/
	public function delete($id = 0){
		if(IS_POST){
			!$id &&	ajax_return(0, "参数有误");
			$sql = "delete from  message where id in ($id)";
			$this->db->query($sql);
			ajax_return(1, "删除成功");
		}
	}
	
	/**
	*提款信 用户发给管理员
	*/
	public function money($start_time = 0 ,$end_time = 0 , $p = 1 ){
		//指定代理商
		$aid = $_SESSION["agent"]["id"];
		
		//只搜索管理员发个普通用户的
		$where = "where a.receiver = 0 AND a.sender = b.id and b.aid = $aid";
		
		//搜索时间
		if (! $start_time) $start_time = date("Y-m-d"); 
		if (! $end_time) $end_time = date("Y-m-d"); 
		if ($start_time > $end_time) $this->error("开始时间不能大于结束时间！");
		$where .= " and a.addtime >= '".$start_time."' and a.addtime <= '".$end_time . " 23:59:59"."'";
		
		//数据分页
		$p = (int)$p <= 0 ? 1 : (int)$p;
		$pize = 15;
		$start = ($p - 1) * $pize;
		
		//统计总数
		$sql = "select count(*) as c from message a,user b " . $where;
		$total = $this->db->query($sql)->row_array();
		$total = $total["c"];

		//分页查询
		$sql = "select a.*,b.username,b.realname  from message a,user b " . $where . " order by a.id DESC limit $start, $pize";
		
		$list = $this->db->query($sql)->result_array();
		
		//分页展示
		$url = "/" . $this->_controller . "/" . $this->_action  . "/" . $start_time. "/" . $end_time . "/";
		$page = page_format($total, $pize, $p, $url);
		
		//分配展示
		$this->assign("list", $list);
		$this->assign("page", $page);
		$this->assign("total", $total);
		$this->assign("begin_time", $start_time);
		$this->assign("end_time", $end_time);
		$this->display();
	}
	
	/**
	* 
	* @param  $id 消息ID
	* 
	* @return
	*/
	public function money_detail($id=0){
		//指定代理商
		$aid = $_SESSION["agent"]["id"];
		!$id &&	$this->error("参数有误");
		
		//查询记录
		$sql = "select a.*,b.username from message a,user b  where a.id = $id and a.sender=b.id and b.aid = $aid";
		$result = $this->db->query($sql)->row_array();
		if(! $result){
			$this->error("记录不存在");
		}
		
		//更新阅读状态
		$sql = "update message set isread = 1 where id = $id and isread = 0";
		$this->db->query($sql);
		
		$this->assign("result", $result);
		$this->display();
		
	}
}
